/**
 * Created by msi on 2017/6/1.
 */
$(document).ready(function($) {
    var page = 1;

    $(".pages").click(function(event){
    var page = $(event.target).html();
        getPage(page);
    });

    $("#edit").click(function(event) {
        event.preventDefault();
        $(".content-main").css("display", "none");
        $(".set-post").css("display", "");
        $(".set-post").attr('data-contentid', $(".post").attr('data-contentid'));
        $(".set-post>div>div>h3").html('EDIT POST');
        $("#post-submit").attr('value', 'CHANGE POST');
        $("#post-id").val($(".post").attr('data-contentid'));
        $("#form-type").val('edit');
        $(".input-title").val($(".content-title").html());
        $("#select-file").val(null);
        $("#file-name").html('');
        $(".input-description").val($(".content-description").html());
    });

    $("#delete").click(function(event) {
        event.preventDefault();
        var id = $('.post').attr('data-contentid');
        console.log(id);
        $.ajax({
            url:  'post'+ '?' + $.param({"id": id}),
            type: 'DELETE',
            dataType: "",
            success: window.location.reload()
        });
    });

    $(".more").click(function(event) {
        event.preventDefault();
        var id = $(this).parent().parent().attr('data-contentid');
        $.getJSON("/post",{id:id},function(data){
            var post = data['post'];
            $(".content-grid-single>h3").html(post['title']);
            $(".content-grid-single>h4>span").html(post['date']);
            if(post['image']==null){
                $(".content-grid-single>img").css("display", "none");
            }
            else{
                $(".content-grid-single>img").css("display", "block");
                $(".content-grid-single>img").attr("src", "img/"+post['image']);
            }
            $(".content-grid-single>p").html(post['description']);
            if(post['auth']!=true){
                $(".auth").css("display", "none");
            }
            else{
                $(".auth").css("display", "block");
            }
        });
        $(".content-main").css("display", "none");

        $(".post").css("display", "");
        $(".post").attr('data-contentid', id);
    });


    var getPage = function(page){
        var keyword = $("#keyword").val();
        var jsondata;
        if(keyword==null||keyword==""){
            jsondata = {page:page};
        }
        else{
            jsondata = {page:page, keyword:keyword};
        }
        $.getJSON("/blog",jsondata,function(data){
            $(".user").html(data['username']);
            var totalPage = data['total page'];
            // var pageTag = "";
            $(".pages>ul").html("");
            var modelpage = $(".node-page>li");
            for(var i=1; i<= totalPage; i++){
                var pageitem = modelpage.clone(true);
                pageitem.find("a").html(i);
                if(i==page){
                    pageitem.addClass('active');
                }
                $(".pages>ul").append(pageitem);
            }
            $(".list").html("");
            var modellist = $(".node-list>div");
            $.each(data['posts'], function (i, post)
            {
                var list = modellist.clone(true);
                list.attr("data-contentid", post['id']);
                list.find("h3>a").html(post['title']);
                list.find("h4").html(post['date']+", by "+post['author']);
                list.find("p").html(post['description']);
                if(post['image']==null){
                    list.find("img").css("display", "none");
                }
                else{
                    list.find("img").css("display", "block");
                    list.find("img").attr("src", "img/"+post['image']);
                }
                $(".list").append(list);
            });
        });
    };
    getPage(1);

    $(".newpost").click(function(event) {
        event.preventDefault();
        $(".content-main").css("display", "none");
        $(".set-post").css("display", "");
        $(".set-post>div>div>h3").html('NEW POST');
        $("#post-submit").attr('value', 'CREATE POST');
        $("#form-type").val('init');
        $(".banner-links>ul>li").removeClass("active");
        $(".newpost").addClass("active");
        $(".post").attr('data-contentid', '');
        $(".input-title").val('');
        $("#select-file").val(null);
        $("#file-name").html('');
        $(".input-description").val('');
    });


    $(".scroll").click(function(event) {
        event.preventDefault();
        $('html,body').animate({
            scrollTop: $(this.hash).offset().top
        }, 900);
    });


    $(".view").click(function(event) {
        event.preventDefault();
        $(".content-main").css("display", "none");
        $(".lists").css("display", "");
        $(".banner-links>ul>li").removeClass("active");
        $(".view").addClass("active");
        $(".post").attr('data-contentid', '');
    });

    $('#select-file').change(function(){
        var str=$(this).val();
        var arr=str.split('\\');
        var filename=arr[arr.length-1];
        $("#file-name").html(filename);
    });

    $("#search").click(function(event){
        event.preventDefault();
        getPage(1);
        $(".view").trigger("click");
    });

    $("#post-submitt").click(function(event){
        event.preventDefault();
        var id = $(".post").attr('data-contentid');
        if(id==''){
            $.ajax({
                url:  'post',
                type: 'POST',
                data: new FormData($("#post")),
                success: window.location.reload()
            });
        }
        else{
            $.ajax({
                url:  'post',
                type: 'PUT',
                data: (new FormData($("#post")))+{'id':id},
                success: window.location.reload()
            });
        }
    });

    $(".quit").click(function (event) {
        $.ajax({
            url:  '/home',
            type: 'DELETE'
        });
    });

    $().UItoTop({
        easingType: 'easeOutQuart'
    });
});
